package com.itheima.algorithm.datastructure;

/**
 * 单链表
 */
public class ListNode {
    /**
     * 链表值
     */
    int value;
    /**
     * next指针
     */
    ListNode next;


    public ListNode(int value, ListNode next) {
        this.value = value;
        this.next = next;
    }

    @Override
    public String toString() {
        StringBuilder sb =new StringBuilder(64);
        sb.append("[");
        ListNode p =this;
        while (p!=null){
            sb.append(p.value);
            if(p.next!=null){
                sb.append(",");
            }
            p = p.next;
        }
        sb.append("]");
        return sb.toString();
    }


    public static ListNode of(int... element){
        if(element.length == 0){
            return null;
        }
        ListNode p =null;
        for (int i = element.length-1; i >=0 ; i--) {
            p = new ListNode(element[i],p);
        }
        return p;
    }


}
